package array_string_try;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ArrayLearn {
    public static void reverse(Integer[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            // 交换元素
            int temp = array[left];
            array[left] = array[right];
            array[right] = temp;
            // 移动指针
            left++;
            right--;
        }
    }
    public static void main(String[] args)
    {
        double[] myList = {1.9, 2.9, 3.4, 3.5};

        // 打印所有数组元素
        for (double v : myList) {
            System.out.println(v + " ");
        }

        // 计算所有元素的总和
        double total = 0;
        for (double v : myList) {
            total += v;
        }

        System.out.println("Total is " + total);

        // 查找最大元素
        double max = myList[0];
        for (int i = 1; i < myList.length; i++)
        {
            if (myList[i] > max) max = myList[i];
        }

        System.out.println("Max is " + max);

        String[] names = {"Alice", "Bob", "Charlie"};
        // 使用自定义比较器按字符串长度排序
        Arrays.sort(names, Comparator.comparingInt(String::length)); //String::length 等价于 s -> s.length()，即提取字符串的长度作为比较依据。
        System.out.println(Arrays.toString(names));
        Arrays.sort(names, Comparator.comparing(String::toString).reversed());
        System.out.println(Arrays.toString(names));
        Arrays.sort(names, Comparator.comparing(String::toString));
        System.out.println(Arrays.toString(names));

        Integer[] numbers = {5, 2, 8, 3, 1, 7, 6, 4};
        //逆序
        reverse(numbers);
        System.out.println(Arrays.toString(numbers));

        // 使用并行排序
        Arrays.parallelSort(numbers,Comparator.reverseOrder());
        System.out.println(Arrays.toString(numbers));

        //asList
        List<Integer> list = Arrays.asList(numbers);
//        list.add(9);
        System.out.println(list);

        List<Integer> newList =new ArrayList<>(Arrays.asList(numbers));
        newList.add(9);
        System.out.println(newList);
    }
}
